package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-27
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    // 登录的方法，使用token存储信息
    @Override
    public String login(UcenterMember member) {
        // 获取登入手机号，密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "手机号或者密码错误, 登录失败");
        }

        // 判断手机号是否正确
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        UcenterMember moblieMember = baseMapper.selectOne(queryWrapper);
        if (StringUtils.isEmpty(moblieMember)) {
            throw new GuliException(20001, "手机号错误，登录失败");
        }

        // 判断密码是否正确
        // 存储到数据库密码是加密的，所以要将自己写的密码加密之后在和数据库的密码比较
        // 加密方式MD5：MD5只能加密，不能解密
        if (!MD5.encrypt(password).equals(moblieMember.getPassword())) {
            throw new GuliException(20001, "密码错误，登录失败");
        }

        // 判断用户是否被禁用
        if (moblieMember.getIsDeleted()) {
            throw new GuliException(20001, "该用户被禁止访问，登录失败");
        }

        // 登录成功，生成一个token字符串
        String jwtToken = JwtUtils.getJwtToken(moblieMember.getId(), moblieMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 获取注册的数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "注册失败！");
        }

        if (StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "验证码为空，注册失败！");
        }
        // 判断验证码
        if (!code.equals(redisTemplate.opsForValue().get(mobile))) {
            throw new GuliException(20001, "验证码错误，注册失败！");
        }

        // 判断手机号是否重复
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Integer integer = baseMapper.selectCount(queryWrapper);
        if (integer > 0) {
            throw new GuliException(20001, "改手机号已被注册，注册失败！");
        }

        // 将数据添加到数据库
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(registerVo, ucenterMember);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setMobile(registerVo.getMobile());
        ucenterMember.setAvatar("https://img.qzonei.com/data/attachment/portal/202206/18/102628u8y59iast4s844a5.jpg");
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        return ucenterMember;
    }

    // 查询某一天的注册人数
    @Override
    public Integer countRegisterDay(String day) {

        return baseMapper.countRegisterDay(day);
    }
}
